package dialog;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.zx.theline.R;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import GsonBean.ProtectMeasuresBean;
import GsonBean.SafetyBean;
import GsonBean.SecurityRiskBean;
import GsonBean.UniteInfomationBean;
import GsonBean.UserBean;
import Utils.MyToast;
import app.MyApplication;
import base.BaseDialog;
import greendao.entity.ProtectMeasures;
import greendao.entity.Safety;
import greendao.entity.SecurityRisk;
import greendao.entity.UnitInformation;
import greendao.entity.User;
import network.BaseCallBack;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zx on 2017/4/26.
 */
//第一次安装程序 加载数据dialog
public class FirstInstallDialog extends BaseDialog {

    //本单位用户信息


    TextView confirm;
    TextView warn;


    static FirstInstallDialog mFirstInstallDialog;

    public FirstInstallDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_firstinstall);

        InitView();
        InitListen();
    }

    @Override
    protected void InitView() {

        confirm = (TextView) findViewById(R.id.confirm);
        warn = (TextView) findViewById(R.id.warn);
        confirm.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void InitData() {


        this.setCanceledOnTouchOutside(false);

        //根据登录用户ID更新本地数据库，该ID部门下的所有人员信息
        DownLoadUserInfomation();

    }

    @Override
    protected void InitListen() {
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FirstInstallDialog.this.dismiss();
            }
        });
    }


    public static FirstInstallDialog getInstance(Context context) {
        if (mFirstInstallDialog == null) {
            mFirstInstallDialog = new FirstInstallDialog(context);
        }
        return mFirstInstallDialog;
    }

    //根据登录用户ID更新本地数据库，该ID部门下的所有人员信息
    private void DownLoadUserInfomation() {

        //根据登录用户ID更新本地数据库，该ID部门下的所有人员信息
        Map<String, String> map = new HashMap<>();
        map.put("ljyyxDwxxId", MyApplication.getmLoginUser().getUserUnitId());
        NetWorkApi.getAllUserInfoByCompanyId(map, new BaseCallBack<UserBean>() {
                    @Override
                    public void Success(Call<UserBean> call, Response<UserBean> response) {
                        try {
                            UserBean mUserBean = response.body();
                            if (mUserBean.getMsg() != null) {
                                List<User> list = new ArrayList<User>();
                                for (UserBean.RowsBean mRowsBean : mUserBean.getRows()) {
                                    User mUser = new User();
                                    mUser.setLjyyxLoginName(mRowsBean.getLjyyxLoginName());
                                    mUser.setLjyyxWorkerName(mRowsBean.getLjyyxWorkerName());
                                    mUser.setLjyyxWorkerTel(mRowsBean.getLjyyxWorkerTel());
                                    mUser.setDownUnitName(mRowsBean.getDownUnitName());
                                    mUser.setLjyyxDwxxId(mRowsBean.getLjyyxDwxxId());
                                    mUser.setLjyyxDwxxName(mRowsBean.getLjyyxDwxxName());
                                    list.add(mUser);
                                }
                                MyApplication.getDaoSession().getUserDao().deleteAll();
                                MyApplication.getDaoSession().getUserDao().insertInTx(list);
                            }
                        } catch (Exception e) {
                            updateFail();
                        }

                        //更新安全风险字典表
                        DownLoadSecurityRisk();
                    }

                    @Override
                    public void Failure(Call<UserBean> call, Throwable t) {
                        updateFail();
                    }

                    @Override
                    public void onFinally() {

                    }


                }

        );

    }

    //更新安全风险字典表
    private void DownLoadSecurityRisk() {

        NetWorkApi.getAllSecurityRisk(new BaseCallBack<SecurityRiskBean>() {

            @Override
            public void Success(Call<SecurityRiskBean> call, Response<SecurityRiskBean> response) {
                try {
                    SecurityRiskBean mBean = response.body();
                    if (mBean.getMsg() != null) {
                        List<SecurityRisk> mList = new ArrayList<>();
                        for (SecurityRiskBean.RowsBean mRowsBean : mBean.getRows()) {
                            SecurityRisk mSecurityRisk = new SecurityRisk();
                            mSecurityRisk.setAqfxId(mRowsBean.getAqfxId());
                            mSecurityRisk.setAqfxDm(mRowsBean.getAqfxDm());
                            mSecurityRisk.setAqfxNr(mRowsBean.getAqfxNr());
                            mSecurityRisk.setAqfxNr(mRowsBean.getAqfxNr());
                            mSecurityRisk.setSgxmId(mRowsBean.getSgxmId());
                            mList.add(mSecurityRisk);
                        }
                        MyApplication.getDaoSession().getSecurityRiskDao().deleteAll();
                        MyApplication.getDaoSession().getSecurityRiskDao().insertOrReplaceInTx(mList);
                    }
                } catch (Exception e) {
                    updateFail();
                }
                //更新安全风险措施
                DownLoadSecurityProtect();
            }

            @Override
            public void Failure(Call<SecurityRiskBean> call, Throwable t) {
                updateFail();
            }

            @Override
            public void onFinally() {

            }
        });
    }

    //更新安全风险措施
    private void DownLoadSecurityProtect() {

        NetWorkApi.getAllProtectMeasures(new BaseCallBack<ProtectMeasuresBean>() {
            @Override
            public void Success(Call<ProtectMeasuresBean> call, Response<ProtectMeasuresBean> response) {
                try {
                    ProtectMeasuresBean mBean = response.body();
                    if (mBean.getMsg() != null) {
                        List<ProtectMeasures> mList = new ArrayList<>();
                        for (ProtectMeasuresBean.RowsBean mRowsBean : mBean.getRows()) {
                            ProtectMeasures mProtectMeasures = new ProtectMeasures();
                            mProtectMeasures.setAqfhcsId(mRowsBean.getAqfhcsId());
                            mProtectMeasures.setAqfhcsDm(mRowsBean.getAqfhcsDm());
                            mProtectMeasures.setAqfhcsNr(mRowsBean.getAqfhcsNr());
                            mProtectMeasures.setAqfhcsNr(mRowsBean.getAqfhcsNr());
                            mProtectMeasures.setSgxmId(mRowsBean.getSgxmId());
                            mList.add(mProtectMeasures);
                        }
                        MyApplication.getDaoSession().getProtectMeasuresDao().deleteAll();
                        MyApplication.getDaoSession().getProtectMeasuresDao().insertOrReplaceInTx(mList);
                    }
                    //获取施工安全问题分类字典表
                    DownLoadSafety();
                } catch (Exception e) {
                    updateFail();
                }
            }

            @Override
            public void Failure(Call<ProtectMeasuresBean> call, Throwable t) {
                updateFail();
            }

            @Override
            public void onFinally() {

            }
        });
    }


    //获取施工安全问题分类字典表

    private void DownLoadSafety() {
        NetWorkApi.getAllSafety(new BaseCallBack<SafetyBean>() {
                                    @Override
                                    public void Success(Call<SafetyBean> call, Response<SafetyBean> response) {
                                        try {
                                            SafetyBean mBean = response.body();
                                            if (mBean.getMsg() != null) {
                                                List<Safety> mList = new ArrayList<Safety>();
                                                for (SafetyBean.RowsBean item : mBean.getRows()) {
                                                    Safety entity = new Safety();
                                                    entity.setSgwtflId(item.getSgwtflId());
                                                    entity.setSgwtdm(item.getSgwtdm());
                                                    entity.setSgwtmc(item.getSgwtmc());
                                                    entity.setDeleteFlag(item.getDeleteFlag());
                                                    entity.setCjrId(item.getCjrId());
                                                    entity.setCjrq(item.getCjrq());
                                                    entity.setXgrId(item.getXgrId());
                                                    entity.setXgrq(item.getXgrq());
                                                    entity.setSfqy(item.getSfqy());
                                                    mList.add(entity);
                                                }
                                                MyApplication.getDaoSession().getSafetyDao().deleteAll();
                                                MyApplication.getDaoSession().getSafetyDao().insertOrReplaceInTx(mList);
                                            }
                                        } catch (Exception e) {
                                            updateFail();
                                        }
                                        //更新所有单位信息
                                        DownLoadUnite();
                                    }

                                    @Override
                                    public void Failure(Call<SafetyBean> call, Throwable t) {
                                        updateFail();
                                    }

                                    @Override
                                    public void onFinally() {


                                    }
                                }
        );

    }

    //更新所有单位信息
    private void DownLoadUnite() {
        NetWorkApi.getAllUniteInfomation(new BaseCallBack<UniteInfomationBean>() {

                                             @Override
                                             public void Success(Call<UniteInfomationBean> call, Response<UniteInfomationBean> response) {
                                                 try {
                                                     UniteInfomationBean mBean = response.body();
                                                     if (mBean.getMsg() != null) {
                                                         List<UnitInformation> mList = new ArrayList<UnitInformation>();
                                                         for (UniteInfomationBean.RowsBean mRows : mBean.getRows()) {
                                                             UnitInformation mInfo = new UnitInformation();
                                                             mInfo.setLjyyxDwxxId(mRows.getLjyyxDwxxId());
                                                             mInfo.setLjyyxDwxxName(mRows.getLjyyxDwxxName());
                                                             mInfo.setLjyyxDwxxType(mRows.getLjyyxDwxxType());
                                                             mInfo.setLjyyxDwxxType(mRows.getLjyyxDwxxType());
                                                             mInfo.setParentId(mRows.getParentId());
                                                             mInfo.setLjyyxDwxxKsdate(mRows.getLjyyxDwxxKsdate());
                                                             mInfo.setDeleteFlag(mRows.getDeleteFlag());
                                                             mInfo.setCjrId(mRows.getCjrId());
                                                             mInfo.setCjrq(mRows.getCjrq());
                                                             mInfo.setXgrId(mRows.getXgrId());
                                                             mInfo.setXgrq(mRows.getXgrq());
                                                             mList.add(mInfo);
                                                         }
                                                         MyApplication.getDaoSession().getUnitInformationDao().deleteAll();
                                                         MyApplication.getDaoSession().getUnitInformationDao().insertOrReplaceInTx(mList);
                                                     }
                                                 } catch (Exception e) {
                                                     updateFail();
                                                 }
                                                 updateFinish();
                                             }

                                             @Override
                                             public void Failure(Call<UniteInfomationBean> call, Throwable t) {
                                                 updateFail();
                                             }

                                             @Override
                                             public void onFinally() {

                                             }
                                         }
        );

    }

    //字典更新失败
    private void updateFail(){
        warn.setText("字典表更新失败，请进入用户中心重新更新");
        confirm.setVisibility(View.VISIBLE);
        this.setCanceledOnTouchOutside(true);
        confirm.setText("确定");
    }

    //字典更新完成
    private void updateFinish(){
        warn.setText("字典表更新完成");
        FirstInstallDialog.this.dismiss();
    }



    @Override
    public void show() {
        super.show();
        InitData();
        WindowManager windowManager = getWindow().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (display.getWidth() * 0.9); //设置宽度
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT; //设置宽度
        getWindow().setAttributes(lp);
    }
}
